package com.dingshang.dsapp.controller;

import com.dingshang.dsapp.dto.ResponseDTO;
import com.dingshang.dsapp.exception.BizException;
import com.dingshang.dsapp.service.BoService;
import com.dingshang.dsapp.service.NavigationService;
import com.dingshang.dsapp.service.PictureService;
import com.dingshang.dsapp.util.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
public class IndexController extends BaseController {

    @Autowired
    private PictureService pictureService;

    @Autowired
    private NavigationService navigationService;

    @Autowired
    private BoService boService;
    
    @RequestMapping("/loadBanner")
    public ResponseDTO loadBanner() {
        
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/homePicture/upload.html")
    public ResponseDTO boHomeUploadPicture(MultipartFile upfile, HttpSession session, HttpServletResponse response) {
        try {
            if (upfile.isEmpty()) {
                return ResponseDTO.fail("上传图片失败");
            }

            String fileName = IDUtils.createImageName("product");
            File uploadedFile = new File(filePath + fileName);
            upfile.transferTo(uploadedFile);

            Map<String, String> map = new HashMap<>(2);
            map.put("filename", fileName);
            map.put("imageUrl", fileUrl + fileName);
            return ResponseDTO.success(map);

        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("上传图片失败");
        }
    }

    @RequestMapping(value = "/fld/images/{filename}", method = RequestMethod.GET)
    public void showImage(@PathVariable("filename") String filename, HttpServletResponse response) {

        response.setContentType("image/png");
        try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(super.filePath + filename));
            OutputStream os = response.getOutputStream()) {
            byte[] buff = new byte[1024];
            int length = -1;
            while (-1 != (length=bis.read(buff))) {
                os.write(buff, 0, length);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/bo/homePicure/add.html")
    public ResponseDTO addHomePicture(String picIntroduce, String picTitle, String picName) {
        pictureService.addHomePicture(picIntroduce, picTitle, picName);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/homePicure/list.html")
    public ResponseDTO listHomePicture() {
        return ResponseDTO.success(pictureService.listHomePicture());
    }

    @RequestMapping("/bo/homePicure/del.html")
    public ResponseDTO delHomePicture(int id) {
        pictureService.delHomePicture(id);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/listAllNav")
    public ResponseDTO listAllNav() {
        return ResponseDTO.success( navigationService.listAllNav());
    }

        @RequestMapping("/listEnableNav")
    public ResponseDTO listEnableNav() {
        return ResponseDTO.success( navigationService.listEnableKeyValue());
    }



    @RequestMapping("/bo/saveAnli")
    @ResponseBody
    public ResponseDTO saveAnli(String bigTitle, String title1, String title2,
                                String picUrl, String zxStyle, String shejishiId, String content) {

        if (null == zxStyle || "".equals(zxStyle)) {
            return ResponseDTO.fail("请选择装修风格");
        }

        boService.addAnli(bigTitle, title1, title2, picUrl, zxStyle, shejishiId, content);

        return ResponseDTO.success();
    }

    @RequestMapping("/bo/listAnli")
    @ResponseBody
    public ResponseDTO listAnli(String zxStyle) {

        if (null == zxStyle || "".equals(zxStyle)) {
            return ResponseDTO.fail("请选择装修风格");
        }

        return ResponseDTO.success(boService.listAnli(zxStyle));
    }

    @RequestMapping("/bo/delKeyValue")
    @ResponseBody
    public ResponseDTO delKeyValue(int id) {
        boService.deleteKeyValue(id);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/saveService")
    @ResponseBody
    public ResponseDTO saveService(String title1, String bigTitle,
                                   String wzcontext, String serverStyle) {

        if (null == serverStyle || "".equals(serverStyle)) {
            return ResponseDTO.fail("请选择类型");
        }

        boService.saveService(title1, bigTitle, wzcontext, serverStyle);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/listServer")
    @ResponseBody
    public ResponseDTO saveService(String serverStyle) {
        if (null == serverStyle || "".equals(serverStyle)) {
            return ResponseDTO.fail("请选择类型");
        }
        return ResponseDTO.success(boService.listServer(serverStyle));
    }

    @RequestMapping("/bo/addShejishi")
    @ResponseBody
    public ResponseDTO saveService(String realName, String anliNum, String jingyan,
                                   String zhiwei, String picUrl, String sjStyle, String jianjie) {

        if (null == sjStyle || "".equals(sjStyle)) {
            return ResponseDTO.fail("请选择分类");
        }

        boService.addShejishi(realName, anliNum, jingyan, zhiwei, picUrl, sjStyle, jianjie);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/shejishilist")
    @ResponseBody
    public ResponseDTO shejishilist(String sjStyle) {
        if (null == sjStyle || "".equals(sjStyle)) {
            return ResponseDTO.fail("请选择分类");
        }
        return ResponseDTO.success(boService.listShejishi(sjStyle));
    }


    @RequestMapping("/bo/addAbout")
    @ResponseBody
    public ResponseDTO addAbout(String wzcontext, String aboutStyle, String bigTitle, String title1) {
        if (null == aboutStyle || "".equals(aboutStyle)) {
            return ResponseDTO.fail("请选择类型");
        }

        boService.addAbout(wzcontext, aboutStyle, bigTitle, title1);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/aboutList")
    @ResponseBody
    public ResponseDTO aboutList(String aboutStyle) {
        if (null == aboutStyle || "".equals(aboutStyle)) {
            return ResponseDTO.fail("请选择类型");
        }
        return ResponseDTO.success(boService.listAbout(aboutStyle));
    }

    @RequestMapping("/yusuan")
    @ResponseBody
    public ResponseDTO yusuan(String phone, String realName,
                              String loupan, String mianji) {

        if (null == phone || "".equals(phone)) {
            return ResponseDTO.fail("请输入手机号码");
        }

        if (phone.length() != 11) {
            return ResponseDTO.fail("请输入正确的手机号码");
        }

        if (null == loupan || "".equals(loupan)) {
            return ResponseDTO.fail("请输入楼盘");
        }

        if (null == mianji || "".equals(mianji)) {
            return ResponseDTO.fail("请输入面积");
        }

        boService.baojia(phone, realName, loupan, mianji);
        return ResponseDTO.success();
    }


    @RequestMapping("/bo/listYusuan")
    @ResponseBody
    public ResponseDTO listYusuan(int currentPage) {
        return ResponseDTO.success(boService.listBaojia(currentPage));
    }

    @RequestMapping("/bo/modifyPwd")
    @ResponseBody
    public ResponseDTO modifyPwd(String pwd, String confimPwd) {

        boService.modifyPwd(pwd, confimPwd);
        return ResponseDTO.success();
    }

    @RequestMapping("/login")
    @ResponseBody
    public ResponseDTO login(String username, String pwd, HttpServletRequest request) {
        request.getSession().setAttribute("loginUser", boService.login(username, pwd));
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/loadShejishi")
    @ResponseBody
    public ResponseDTO loadShejishi() {
        return ResponseDTO.success(boService.loadShejishi());
    }


    @RequestMapping("/bo/listAnliNav")
    @ResponseBody
    public ResponseDTO listAnliNav() {
        return ResponseDTO.success(navigationService.listAnliSecond());
    }

    @RequestMapping("/bo/saveAnliNav")
    @ResponseBody
    public ResponseDTO saveAnliNav(String navName, String navNameEnglish) {
        navigationService.saveAnliNav(navName, navNameEnglish);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/changeAnliStatus")
    @ResponseBody
    public ResponseDTO changeAnliStatus(int id, int status) {
        navigationService.changeAnliStatus(id, status);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/listEnableAnliNav")
    @ResponseBody
    public ResponseDTO listEnableAnliNav() {
        return ResponseDTO.success(navigationService.listEnableAnliSecond());
    }


    @RequestMapping("/bo/modifyShejishi.html")
    @ResponseBody
    public ResponseDTO toModifyShejishi(int id, String realName, String anliNum, String jingyan,
                                   String zhiwei, String picUrl, String sjStyle, String jianjie) {
        boService.modifyShejishi(id, realName, anliNum, jingyan, zhiwei, picUrl, sjStyle, jianjie);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/modifyAnli")
    @ResponseBody
    public ResponseDTO modifyAnli(int id, String bigTitle, String title1, String title2,
                                  String picUrl, String zxStyle, String shejishiId, String content) {

        boService.modifyAnli(id, bigTitle, title1, title2, picUrl, zxStyle, shejishiId, content);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/modifySx")
    @ResponseBody
    public ResponseDTO modifySx(int id, int sxValue) {
        boService.modifySx(id, sxValue);
        return ResponseDTO.success();
    }

    @RequestMapping("/listAppStucf")
    @ResponseBody
    public ResponseDTO listAppStucf(int valueType, int page) {
        return ResponseDTO.success(boService.listAllAppAnli(valueType, page));
    }

}
